package com.bjpowernode.app;

import java.util.Vector;

public class MyApp8 {
    //线程集合
    public static Vector<Runnable> runnables = new Vector<>();
    //线程锁
    public Object lock = new Object();
    //主线程对象
    public Thread threadMain;
    //业务1线程
    Runnable r1 = new Runnable() {
        @Override
        public void run() {
            try {
                //主线程加入
                threadMain.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (lock) {
                for (int i = 1; i <= 10; i++) {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ":" + i);

                    lock.notifyAll();
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //线程集合干掉自己
                boolean remove = runnables.remove(this);
                System.out.println(Thread.currentThread().getName() + ":线程结束了................");
            }
        }
    };


    //业务2线程
    Runnable r2 = new Runnable() {
        @Override
        public void run() {
            try {
                //主线程加入
                threadMain.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            synchronized (lock) {
                for (int i = 20; i <= 40; i++) {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ":" + i);
                    lock.notifyAll();
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //线程集合干掉自己
                runnables.remove(this);
                System.out.println(Thread.currentThread().getName() + ":线程结束了................");
            }
        }
    };


    //监听线程（心跳线程）
    Runnable r3 = new Runnable() {
        @Override
        public void run() {
            try {
                //主线程加入
                threadMain.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            while (true) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lock) {
//                    System.out.println("runnables.size():"+runnables.size());
                    //查看集合
                    if (runnables.size() == 1) {
                        lock.notifyAll();
                    } else if (runnables.size() == 0) {
                        break;
                    }
                }
            }
        }
    };
    public static void main(String[] args) {
        MyApp8 myApp8 = new MyApp8();
        //获取主线程
        myApp8.threadMain = Thread.currentThread();

        //线程准备
        Thread t1 = new Thread(myApp8.r1, "r1");
        Thread t2 = new Thread(myApp8.r2, "r2");
        Thread t3 = new Thread(myApp8.r3, "r3");
        //线程开启【利用主线程join，等待主线程完毕】
        t1.start();
        t2.start();
        t3.start();
        //进入主线程[必须等到这里运行完毕，次线程才能运行]
        runnables.add(myApp8.r1);
        runnables.add(myApp8.r2);
        System.out.println("主线线程结束了............................");
    }


}
